package com.phkj.qdh.ui.fragment.lawinspect

import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.FileProvider
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.commit
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.hjq.toast.ToastUtils
import com.phkj.qdh.bean.FileUploadBean
import com.phkj.qdh.databinding.FragmentLawInspectSelectBinding
import com.phkj.qdh.net.FILE_UPLOAD
import com.phkj.qdh.net.msg
import com.phkj.qdh.ui.adapter.LawInspectTypeSelectAdapter
import com.phkj.qdh.ui.fragment.teaminspect.TeamInspectSubmitFragment
import com.phkj.qdh.ui.fragment.teaminspect.TeamInspectVM
import com.phkj.qdh.ui.fragment.teaminspect.getTeamInspectVMProvider
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import com.phkj.qdh.utils.requestMain
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import rxhttp3.RxHttp
import rxhttp3.toFlowResponse
import java.io.File
import java.text.SimpleDateFormat
import java.util.GregorianCalendar
import java.util.Locale

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 执法查询类型选择
 */
class LawInspectSelectFragment : Fragment() {
    private var param1: String? = null
    private var param2: String? = null

    private lateinit var vm: TeamInspectVM

    private var _binding: FragmentLawInspectSelectBinding? = null
    private val binding get() = _binding!!
    private val lawInspectTypeSelectAdapter by lazy { LawInspectTypeSelectAdapter() }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack()
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentLawInspectSelectBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getTeamInspectVMProvider())[TeamInspectVM::class.java]
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            rvLawType.run {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = lawInspectTypeSelectAdapter
                addItemDecoration(MyItemDecoration(32))
            }
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack("law", FragmentManager.POP_BACK_STACK_INCLUSIVE)
            }
        }


        lawInspectTypeSelectAdapter.setOnItemClickListener { adapter, _, pos ->
            val data = adapter.getItem(pos)
            data?.let {
                parentFragmentManager.commit {
                    addToBackStack("law")
                    replace(id, LawInspectSubmitFragment.newInstance(it, ""))
                    setReorderingAllowed(true)
                }
            }
        }

        launchAndRepeatWithViewLifecycle {
            vm.teamSortList.collectLatest {
                lawInspectTypeSelectAdapter.submitList(it)
            }
        }

        getSort()

    }



    private fun getSort(){
        vm.getTeamSort(2)
    }




    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment LowInspectSelectFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            LawInspectSelectFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}